from django.shortcuts import render, get_object_or_404
from django.core.paginator import Paginator, EmptyPage, InvalidPage
from .models import Pic
# Create your views here.


def pic_list(request):
    """图片列表页"""
    params = request.GET
    clear = 1 if params.get('clear') else 0

    try:
        page = int(params.get('page'))
    except (ValueError, TypeError):
        page = 1

    all_pics = Pic.objects.all().order_by('-id')
    paginator = Paginator(all_pics, 10)

    pics = paginator.get_page(page)

    return render(request, 'pic/pic_list.html', {
        'pics': pics,
    })


def pic_detail(request, pic_id):
    """图片详情页"""
    pic = get_object_or_404(Pic, id=pic_id)

    return render(request, 'pic/pic_detail.html', {
        'pic': pic,
    })
